package com.stars.empoly.vmodel

import android.app.Activity
import android.app.Application
import android.content.Intent
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import com.stars.empoly.BR
import com.stars.empoly.activity.BankActivity
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.model.InfoBean
import com.stars.empoly.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.UiSwitch

/**
 *
 *on 2020/9/22
 */
class WithdrawalVModel(application: Application): BViewModel<BRepository>(application) {

    var canUseMoney: MutableLiveData<String> = MutableLiveData()

    var tips: MutableLiveData<String> = MutableLiveData()


    var infoBank:MutableLiveData<InfoBean> = MutableLiveData()
    var type=0


    @Bindable
    var money: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.money)
        }


    fun goWithdrawal(){

        if(type==0){
            toast("请去绑定银行卡信息")
            return
        }
        if(TextUtils.isEmpty(money)){
            toast("请输入提现金额")
            return
        }
        if(money!!.toDouble()<1){
            toast("单笔不低于1元")
            return
        }

        repository().rxLreq(NetEngine.service.withdraw(money, infoBank.value!!.name,infoBank.value!!.bank,
                infoBank.value!!.kbank,infoBank.value!!.number))
                .doOnNext {
                    if (it.ok(true)) {
                        var con = repository().context as Activity
                        con.setResult(Activity.RESULT_OK, Intent())
                        con.finish()
                    }
                }
                .subscribe()

    }

    fun getBankInfo(){//获取银行信息
        repository().rxLreq(NetEngine.service.getInfo("4"))
                .doOnNext {
                    if (it.ok(false)) {
                        var bean=it.data() as InfoBean
                        if(null!=bean){
                            type=1
                            infoBank.value=bean
                        }else{
                            type=0
                        }

                    }
                }
                .subscribe()

    }

    fun reqTips(){//获取提示语

        repository().rxLreq(NetEngine.service.getInfo("5"))
                .doOnNext {
                    if (it.ok(false)) {
                        var bean=it.data() as InfoBean
                        tips.value=bean.tips }
                }
                .subscribe()
    }

    fun goBank(){
        UiSwitch.singleRes(repository().context as Activity,
                BankActivity::class.java,MIntentAction.REQUEST_CODE_ONE)

    }

    fun toAllMoney(){//全部提现
        money= canUseMoney.value

    }
}
